package com.example.oldtest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
import ru.esteru.selenium.factory.WebDriverFactory;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.fail;

public class Mozilla {
    private WebDriver driver;

    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());
        //  driver = new FirefoxDriver();   //comment firefox driver old call version
        // попробуем так
        baseUrl = "http://localhost:8888/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    //поехали!!!

    @Test
    public void LoginLogout() throws Exception {
        goToPage("/php4dvd/");
        login("admin", "admin");
        logout();
    }

    @Test
    public void changeEmail() {
        goToPage("/php4dvd/");
        login("admin", "admin");
        goToUserProfile();
        changeEmail("admin@admin.ru");
        logout();
    }

    @Test
    public void addGuestUser() {
        goToPage("/php4dvd/");
        login("admin", "admin");
        goToManagment();
        addGuestUser("Simpleuser", "user1@test.ru", "12345");
        logout();

    }


    @Test
    public void addEditorUser() {
        goToPage("/php4dvd/");
        login("admin", "admin");
        goToManagment();
        addEditorUser("Moderator", "user2@test.ru", "54321");
        logout();

    }

    @Test
    public void DeleteCurrUser() {
        goToPage("/php4dvd/");
        login("admin", "admin");
        goToManagment();
        deleteUser();
        logout();

    }

    @Test
    public void addmovie() {
        goToPage("/php4dvd/");
        login("admin", "admin");
        movieMenu();
        addNames("Ледниковый период 4", "Ледниковый период 4: Континентальный дрейф");
        addYearRaitDuration("2012", "10", "94");
        submit();
        logout();

    }

    // декомпозиция

    private void submit() {
        driver.findElement(By.id("submit")).click();
    }

    private void addYearRaitDuration(String year, String raiting, String duration) {
        driver.findElement(By.name("year")).clear();
        driver.findElement(By.name("year")).sendKeys(year);
        driver.findElement(By.name("rating")).clear();
        driver.findElement(By.name("rating")).sendKeys(raiting);
        driver.findElement(By.name("duration")).clear();
        driver.findElement(By.name("duration")).sendKeys(duration);
    }


    private void addNames(String name, String name2) {
        driver.findElement(By.name("name")).clear();
        driver.findElement(By.name("name")).sendKeys(name);
        driver.findElement(By.name("aka")).clear();
        driver.findElement(By.name("aka")).sendKeys(name2);
    }

    private void movieMenu() {
        driver.findElement(By.cssSelector("h1")).click();
        driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();

    }

    /*удаление одного существующего юзера, кроме админа. Если пользователей несколько, то удаляется последний добавленный  */
    private void deleteUser() {
        driver.findElement(By.cssSelector("img[alt=\"Remove\"]")).click();
        driver.switchTo().alert().accept();

    }

    private void addEditorUser(String username, String email, String password) {
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys(email);
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("password2")).clear();
        driver.findElement(By.id("password2")).sendKeys(password);
        new Select(driver.findElement(By.name("permission"))).selectByVisibleText("Editor");
        driver.findElement(By.name("submit")).click();
    }


    private void addGuestUser(String username, String email, String password) {
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys(email);
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("password2")).clear();
        driver.findElement(By.id("password2")).sendKeys(password);
        new Select(driver.findElement(By.name("permission"))).selectByVisibleText("Guest (view only)");
        driver.findElement(By.name("submit")).click();
    }

    private void goToManagment() {
        driver.findElement(By.linkText("User management")).click();
    }


    private void changeEmail(String email) {
        driver.findElement(By.id("email")).clear();
        driver.findElement(By.id("email")).sendKeys(email);
        driver.findElement(By.name("submit")).click();
    }

    private void goToUserProfile() {
        driver.findElement(By.linkText("My profile")).click();
    }

    private void logout() {
        //logout
        driver.findElement(By.linkText("Log out")).click();
        driver.switchTo().alert().accept();
    }

    private void login(String username, String password) {
        //login
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys(password);
        driver.findElement(By.name("submit")).click();
    }

    private void goToPage(String url) {

        driver.get(baseUrl + url);
    }

    //кончились декомпозиции

    @After
    public void tearDown() throws Exception {
        //driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }


}
